
package com.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.common.domain.Pages.PageResult;
import com.common.domain.pojo.Result;
import com.user.domain.dto.OperatePageParams;
import com.user.domain.dto.PermissionDto;
import com.user.domain.dto.RoleDto;
import com.user.domain.params.RolePages;
import com.user.domain.dto.UserDto;
import com.user.domain.params.UserPages;
import com.user.domain.pojo.Role;
import com.user.domain.pojo.User;
import com.user.domain.vo.RoleVo;
import com.user.domain.vo.UserOperateLogVo;
import com.user.domain.vo.UserVo;
import com.user.service.IPermissionService;
import com.user.service.IRoleService;
import com.user.service.IUserOperateLogsService;
import com.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/system")
@RequiredArgsConstructor
public class SystemController {

    final IUserService iUserService;

    final IRoleService iRoleService;

    final IPermissionService iPermissionService;

    final IUserOperateLogsService iUserOperateLogsService;

    // 获取用户列表
    @GetMapping("/user/list")
    public Result<PageResult<User>> userList(UserPages userPages){
        Page<User> page = Page.of(userPages.getPageOn(), userPages.getPageSize());
        List<User> list = iUserService.list(page,new LambdaQueryWrapper<User>()
                .eq(userPages.getStatus() != null , User::getStatus, userPages.getStatus())
                .like(!StrUtil.isEmptyIfStr(userPages.getName()), User::getName, userPages.getName())
        );
        list.forEach(users -> users.setPassword(null));
        return Result.success(new PageResult<>(page.getTotal(), list));
    }

    // 获取用户详细
    @GetMapping("/user/{id}")
    public Result<UserVo> userDetail(@PathVariable("id") Long id){
        UserVo userVo = iUserService.getUserVo(id);
        return Result.success(userVo);
    }

    // 新增用户
    @PostMapping("/user")
    public Result<String> addUser(@RequestBody UserDto userDto){
        iUserService.addUsers(userDto);
        return Result.success();
    }

    // 修改用户信息
    @PutMapping("/user")
    public Result<String> updateUser(@RequestBody UserDto userDto){
        iUserService.updateUsers(userDto);
        return Result.success();
    }

    // 获取角色列表
    @GetMapping("/user/role/list")
    public Result<List<Role>> roleList(){
        List<Role> list = iRoleService.list(new LambdaQueryWrapper<Role>()
                .eq(Role::getStatus,1));
        // 过滤掉超级管理员角色
        list.removeIf(role -> role.getName().equals("admin"));
        return Result.success(list);
    }

    // 根据id批量删除用户
    @DeleteMapping("/user")
    public Result<String> deleteUser(@RequestParam(value = "ids") List<Long> ids){
        iUserService.removeUsers(ids);
        return Result.success();
    }

    // 分页获取角色信息
    @GetMapping("/role/page")
    public Result<PageResult<Role>> roleList(RolePages rolePages){
        Page<Role> page = Page.of(rolePages.getPageOn(), rolePages.getPageSize());
        List<Role> list = iRoleService.list(page,new LambdaQueryWrapper<Role>()
                .eq( rolePages.getStatus() != null, Role::getStatus, rolePages.getStatus())
                .like(!StrUtil.isEmptyIfStr(rolePages.getName()), Role::getName, rolePages.getName())
                .or()
                .like(!StrUtil.isEmptyIfStr(rolePages.getName()), Role::getRemark, rolePages.getName())
        );
        return Result.success(new PageResult<>(page.getTotal(), list));
    }

    // 获取权限列表
    @GetMapping("/permission/list")
    public Result<List<PermissionDto>> permissionList(){
        List<PermissionDto> list = iPermissionService.getPermissionList();
        return Result.success(list);
    }

    // 根据id获取角色信息
    @GetMapping("/role/{id}")
    public Result<RoleVo> roleDetail(@PathVariable("id") Long id){
        RoleVo role = iRoleService.getRoleDetail(id);
        return Result.success(role);
    }

    // 新增角色信息
    @PostMapping("/role")
    public Result<String> addRole(@RequestBody RoleDto roleDto){
        iRoleService.addRole(roleDto);
        return Result.success();
    }

    // 修改角色信息
    @PutMapping("/role")
    public Result<String> updateRole(@RequestBody RoleDto roleDto){
        iRoleService.updateRole(roleDto);
        return Result.success();
    }

    // 根据id批量删除角色
    @DeleteMapping("/role")
    public Result<String> deleteRole(@RequestParam(value = "ids") List<Long> ids){
        iRoleService.deleteRoles(ids);
        return Result.success();
    }

    // 分页获取操作日志列表
    @GetMapping("/operate/log/page")
    public Result<PageResult<UserOperateLogVo>> operateLogList(OperatePageParams operatePageParams){
        PageResult<UserOperateLogVo> pageResult = iUserOperateLogsService.pageOperateLogList(operatePageParams);
        return Result.success(pageResult);
    }

    // 获取操作日志详细信息
    @GetMapping("/operate/log/{id}")
    public Result<UserOperateLogVo> operateLogDetail(@PathVariable("id") Long id){
        UserOperateLogVo userOperateLogVo = iUserOperateLogsService.operateLogDetailById(id);
        return Result.success(userOperateLogVo);
    }

}